package org.putrpctorest.service.impl.copy;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.putrpctorest.container.Container;

public class ServicesImpl {
    // implements Services {

    private String id;

    private String basePath;

    private Set<ResourceImpl> resources = new HashSet<ResourceImpl>();

    private Set<String> produces = new HashSet<String>();

    private Set<String> consumes = new HashSet<String>();

    private Container container;

    public ServicesImpl(String id) {
        this.id = id;
    }

    public ServicesImpl() {
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    // @Override
    public String getBasePath() {
        return basePath;
    }

    // @Override
    public Set<ResourceImpl> getResources() {
        return Collections.unmodifiableSet(resources);
    }

    // @Override
    public ResourceImpl getResource(String id) {
        for (ResourceImpl resource : getResources()) {
            if (resource.getId().equals(id)) {
                return resource;
            }
        }
        return null;
    }

    public void setResources(Set<ResourceImpl> resources) {
        this.resources = new HashSet<ResourceImpl>();
        for (ResourceImpl resource : resources) {
            resource.setServicesDefinitions(this);
            this.resources.add(resource);
        }
    }

    public void addResource(ResourceImpl resource) {
        if (resource.getServicesDefinitions() == null) {
            resource.setServicesDefinitions(this);
        }
        resources.add(resource);
    }

    // @Override
    public Set<String> getProduces() {
        return Collections.unmodifiableSet(produces);
    }

    public void addProduces(String mediaType) {
        produces.add(mediaType);
    }

    public void setProduces(Set<String> mediaTypes) {
        produces = new HashSet<String>(mediaTypes);
    }

    // @Override
    public Set<String> getConsumes() {
        return Collections.unmodifiableSet(consumes);
    }

    public void addConsumes(String mediaType) {
        consumes.add(mediaType);
    }

    public void setConsumes(Set<String> mediaTypes) {
        consumes = new HashSet<String>(mediaTypes);
    }

    // @Override
    public Container getContainer() {
        return container;
    }

    public void setContainer(Container container) {
        this.container = container;
    }

    public String getId() {
        if (getContainer() == null) {
            return id;
        } else {
            return getContainer().getId();
        }
    }

    @Override
    public String toString() {
        return "[ServiceDefinitions " + getId() + "]";
    }

    @Override
    public boolean equals(Object o) {
        if (getId() != null) {
            if (o instanceof ServicesImpl) {
                if (getId().equals(((ServicesImpl) o).getId())) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            if (((ServicesImpl) o).getId() == null) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

}
